/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BUS;

import DAO.LoaiMatHangDAO;
import DTO.LoaiMatHangDTO;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author HuuTri
 */
public class LoaiMatHangBUS {
    /**
     * get new maloaimathang
     * @return 
     */
    public static int getNewMaLoaiMatHang() {
        return LoaiMatHangDAO.getNewMaLoaiMatHang();
    }
    
    /**
     * check available of maloaimathang
     * @param id
     * @param maloaimathang
     * @return 
     */
    public static boolean checkMaLoaiMatHang(String id, String maloaimathang) {
        LoaiMatHangDTO loaimathang = new LoaiMatHangDTO();
        loaimathang.setId(Integer.parseInt(id));
        loaimathang.setMaLoaiMatHang(maloaimathang);
        
        return LoaiMatHangDAO.checkMaLoaiMatHang(loaimathang);
    }
    
    /**
     * set value of tablemodel record
     * @param tableModel
     * @param row
     * @param columnName
     * @param value 
     */
    public static void setValue(TableModel tableModel, int row, 
                                String columnName, Object value) {
         int index = 0;
         if (row < 0) return;
         
         switch (columnName) {
            case LoaiMatHangDTO.ID: 
                tableModel.setValueAt(value, row, index);
                break;
            case LoaiMatHangDTO.MA_LOAI_MAT_HANG: 
                tableModel.setValueAt(value, row, index + 1);
                break;
            case LoaiMatHangDTO.TEN_LOAI_MAT_HANG: 
                tableModel.setValueAt(value, row, index + 2);
                break;
            case LoaiMatHangDTO.MO_TA: 
                tableModel.setValueAt(value, row, index + 3);
                break;
         }
    }
    
    /**
     * get value by columnName
     * @param tableModel
     * @param row
     * @param columnName
     * @return 
     */
    public static Object getValue(TableModel tableModel, int row, String columnName) {
         int index = 0;
         Object value = " ";
         if (row < 0) return " ";
         
         switch (columnName) {
            case LoaiMatHangDTO.ID: 
                value = tableModel.getValueAt(row, index);
                break;
            case LoaiMatHangDTO.MA_LOAI_MAT_HANG: 
                value = tableModel.getValueAt(row, index + 1);
                break;
            case LoaiMatHangDTO.TEN_LOAI_MAT_HANG: 
                value = tableModel.getValueAt(row, index + 2);
                break;
            case LoaiMatHangDTO.MO_TA: 
                value = tableModel.getValueAt(row, index + 3);
                break;
         }
         if (value != null)
            return value;
         else 
            return  " ";
    }
    
    /**
     * get table Model
     * @return 
     */
    public static DefaultTableModel getTableModel() {
        return LoaiMatHangDAO.getTableModel();
    }

    /**
     * insert record
     * @param sodinhdanh
     * @param maloaimathang
     * @param tenloaimathang
     * @param mota
     * @return 
     */
    public static boolean insertRecord(String sodinhdanh, String maloaimathang, 
                                        String tenloaimathang, String mota) {
        LoaiMatHangDTO loaimathang = new LoaiMatHangDTO(
                                 Integer.parseInt(sodinhdanh),
                                 maloaimathang,
                                 tenloaimathang,
                                 mota
                                      );
        
        return LoaiMatHangDAO.insert(loaimathang);
    }

    /**
     * delete record by id
     * @param id
     * @return 
     */
    public static boolean deleteRecord(String id) {
        LoaiMatHangDTO loaimathang = new LoaiMatHangDTO();
        loaimathang.setId(Integer.parseInt(id));
        
        return LoaiMatHangDAO.delete(loaimathang);
    }

    /**
     * update record by id
     * @param sodinhdanh
     * @param maloaimathang
     * @param tenloaimathang
     * @param mota
     * @return 
     */
    public static boolean updateRecord(String sodinhdanh, String maloaimathang, 
                                        String tenloaimathang, String mota) {
         LoaiMatHangDTO loaimathang = new LoaiMatHangDTO(
                                 Integer.parseInt(sodinhdanh),
                                 maloaimathang,
                                 tenloaimathang,
                                 mota
                                      );
        
        return LoaiMatHangDAO.update(loaimathang);
    }

  
}
